package com.itheima;

import com.bean.Student;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.util.ArrayList;

/*
    实现查看功能
 */
public class ListStudentServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //1.创建字符输入流对象，关联读取的文件
        BufferedReader br = new BufferedReader(new FileReader("d:\\stu.txt"));

        //2.创建集合对象，用于保存Student对象
        ArrayList<Student> list = new ArrayList<>();

        //3.循环读取文件中的数据，将数据封装到Student对象中。再把多个学生对象添加到集合中
        String line;
        while((line = br.readLine()) != null) {
            resp.setContentType("text/html;charset=UTF-8");
            //获取输出流对象
            PrintWriter pw = resp.getWriter();
            pw.write(line);
        }

//        while((line = br.readLine()) != null) {
//            //张三,23,95
//            Student stu = new Student();
//            String[] arr = line.split(",");
//            stu.setUsername(arr[0]);
//            stu.setAge(Integer.parseInt(arr[1]));
//            stu.setScore(Integer.parseInt(arr[2]));
//            list.add(stu);
//        }

        //4.遍历集合，将数据响应给浏览器
//        resp.setContentType("text/html;charset=UTF-8");
        //获取输出流对象
//        PrintWriter pw = resp.getWriter();

//        for(Student s : list) {
//            pw.write(s.getUsername() + "," + s.getAge() + "," + s.getScore());
//            pw.write("<br>");
//        }
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req,resp);
    }
}
